package com.shejiao.goods.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shejiao.goods.dao.BrandMapper;
import com.shejiao.goods.pojo.Brand;
import com.shejiao.goods.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class BrandServiceImpl implements BrandService {

    @Autowired
    private BrandMapper brandMapper;

    @Autowired
    private RedisTemplate redisTemplate;


    @Override
    public List<Brand> findAll() {
//        Brand orderItem = new Brand();
//        redisTemplate.boundHashOps("Cart_"+"itit").put(11223,orderItem);
//

        return brandMapper.selectAll();
    }

    @Override
    public Brand findById(Integer id) {

        return brandMapper.selectByPrimaryKey(id);

//
//        String key="brand"+id;
//        Brand brand = (Brand)redisTemplate.boundValueOps(key).get();//取redis
//        if(redisTemplate.hasKey(key))
//            return brand;
//        else{
//            Brand one=brandMapper.selectByPrimaryKey(id);   //取数据库
//            System.out.println("第一次查询数据库");
//            redisTemplate.boundValueOps(key).set(one);      //存redis
//            if(one==null)
//                redisTemplate.expire(key,30, TimeUnit.SECONDS);
//
//            return one;
//        }
    }

    @Override
    public void add(Brand brand) {
        brandMapper.insertSelective(brand);//忽略空值
    }

    @Override
    public void update(Brand brand) {
        brandMapper.updateByPrimaryKeySelective(brand);
    }

    @Override
    public void delete(Integer id) {
        brandMapper.deleteByPrimaryKey(id);
    }

    //构造条件
    public Example createExample(Brand brand){
        Example example=new Example(Brand.class);
        Example.Criteria criteria = example.createCriteria();

        if(brand!=null){
            //构造:name like "%xxx%"
            if(!StringUtils.isEmpty(brand.getName())){
                criteria.andLike("name","%"+brand.getName()+"%");
            }
            //构造:letter = "xxx"
            if(!StringUtils.isEmpty(brand.getLetter())){
                criteria.andEqualTo("letter",brand.getLetter());
            }
        }

        return example;
    }

    //多字段查询
    @Override
    public List<Brand> findList(Brand brand) {
        //构造条件
        Example example=createExample(brand);
        return brandMapper.selectByExample(example);
    }

    @Override
    public PageInfo<Brand> findPage(Integer page, Integer size) {
        //startPage后面必须紧跟集合
        PageHelper.startPage(page,size);
        List<Brand> brands = brandMapper.selectAll();
        return new PageInfo<Brand>(brands);
    }

    @Override
    public PageInfo<Brand> findPage(Brand brand, Integer page, Integer size) {
        //分页
        PageHelper.startPage(page,size);
        //example
        Example example=createExample(brand);
        List<Brand> brands = brandMapper.selectByExample(example);
        //封装PageInfo
        return new PageInfo<Brand>(brands);
    }


}
